package com.order.utils;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.baomidou.mybatisplus.generator.query.SQLQuery;

import java.nio.file.Paths;
import java.util.Collections;


/**
 * 代码生成器
 */
public class CodeGenerator {


    static String url ="jdbc:mysql://192.168.63.161:3306/cloud_goods?useUnicode=true&useSSL=false&characterEncoding=utf8&remarks=true&useInformationSchema=true";


    public static void main(String[] args) {

        String projectPath = Paths.get(System.getProperty("user.dir")).toString();
        String modulePath = projectPath + "/cloud-admin"; // 替换为实际子项目目录名//

        FastAutoGenerator.create(url, "root", "bo81911")
                .globalConfig(builder -> builder
                        .author("zhaobo") // 设置作者
                        .enableSwagger() // 开启 swagger 模式
                        .outputDir( modulePath + "/src/main/java")
                        .commentDate("yyyy-MM-dd")
                        .disableOpenDir() // 关键配置：禁止生成完成后打开目录

                )
                .dataSourceConfig(builder ->
                        builder.databaseQueryClass(SQLQuery.class)
                                .typeConvert(new MySqlTypeConvert())
                                .dbQuery(new MySqlQuery())
                )
                .packageConfig(builder ->
                        builder.parent("com.order") // 设置父包名
                                .entity("entity")
                                .mapper("mapper")
                                .service("service")
                                .serviceImpl("service.impl")
                                .xml("mapper.xml")
                                .pathInfo(Collections.singletonMap(OutputFile.xml,modulePath + "/src/main/resources/mapper")) // 设置mapperXml生成路径
                )
                .strategyConfig(builder ->
                        builder.addInclude("sys_user") // 设置需要生成的表名
                                .addTablePrefix("sys_") // 设置过滤表前缀
                                .entityBuilder()
                                .enableLombok()  // 关键配置：启用Lombok注解
                                .enableTableFieldAnnotation()
                                .superClass("com.common.entity.BaseEntity") // 关键配置：指定父类
                                .addSuperEntityColumns("create_time", "update_time","create_by","update_By","del_flag") // 添加父类公共字段(这些字段将不在实体中生成)
                                // 添加表字段填充
                                .addTableFills(new Column("create_time", FieldFill.INSERT))
                                .addTableFills(new Property("update_time", FieldFill.INSERT_UPDATE))
                                // 逻辑删除字段名(数据库)
                                .logicDeleteColumnName("del_flag")
                                .controllerBuilder()
                                .enableRestStyle() // 关键配置：启用REST风格控制器
                                .serviceBuilder()
                                .formatServiceFileName("%sService") // 3.5.1+版本支持
                                .formatServiceImplFileName("%sServiceImpl")

                )
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
}
